<?php

namespace App\Http\Controllers\Home\Hr;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Home\Notice as NoticeModel;
use Notice;
use Tools;
use Auth;
use Illuminate\Support\Facades\DB;

class NoticeController extends Controller
{
    protected $passField = [
        'title','content','start_time','end_time','status'
    ];

    public function notice(Request $request)
    {
        $between = Tools::getBetween(false,'Y-m-d');

        $page = $request->input('page');

        $page_info = Tools::page_helper($page,config('base.home_limit.base_limit.render_limit'),config('base.home_limit.base_limit.limit'));

        $opt = [
            'status'=>['!=','-1']
        ];

        $list = Notice::getList($between,$page_info['skip'],$page_info['limit'],$opt);

        $count = Notice::getCount($between,$opt);

        $num = intval(ceil($count/config('base.home_limit.base_limit.limit')));

        foreach ($between as $k => $item){

            $between[$k] = date('Y-m-d',strtotime($item->toDateTimeString()));

        }
        $admin = Db::table('frame')->where('level',1)->value('uid');

        return view('Home.User.Hr.Notice.notice',compact('list','page','num','between','admin'));
    }

    public function notice_add(Request $request){
        if($request->isMethod('post')){
            //验证
            $this->validate(request(), [
                'title' => 'required|string|max:100',
                'content' => 'required|string',
                'start_time' => 'required|string',
                'end_time' => 'required|string',
                'status' => 'required|string'
            ],[],[
                'title' => '标题',
                'content' => '内容',
                'start_time' => '推送开始时间',
                'end_time' => '推送结束时间',
                'status' => '状态',
            ]);

            //数据库操作
            $data = [];
            foreach ($request->all() as $key => $val){
                if (!is_numeric($val)){	$val = clean($val);}
                if(in_array($key,$this->passField) && $val){
                    $val = trim($val);
                    $data[$key] = $val;
                }
            }

            NoticeModel::create($data);

            return ['code'=>1];
            //返回结果
        }

        return view('Home.User.Hr.Notice.add');
    }

    public function notice_edit(NoticeModel $obj){
        if(request() ->isMethod('post')){

            //验证
            $this->validate(request(), [
                'title' => 'required|string|max:100',
                'content' => 'required|string',
                'start_time' => 'required|string',
                'end_time' => 'required|string',
                'status' => 'required|string'
            ],[],[
                'title' => '标题',
                'content' => '内容',
                'start_time' => '推送开始时间',
                'end_time' => '推送结束时间',
                'status' => '状态',
            ]);

            //数据库操作
            foreach (request()->all() as $key => $val){
                if (!is_numeric($val)){	$val = clean($val);}
                if(in_array($key,$this->passField) && $val){
                    $val = trim($val);
                    $obj->$key = $val;
                }
            }

            $obj->save();

            //返回结果
            return ['code'=>1];
        }
        
        return view('Home.User.Hr.Notice.edit',compact('obj'));
    }

    public function notice_detail(NoticeModel $obj){

        return view('Home.User.Hr.Notice.detail',compact('obj'));
    }
    /*删除公告*/
    public function notice_del(NoticeModel $obj){
        if(Auth::user()->hasAnyRole(['admin']) || Auth::id()==$obj->uid){

            $obj->status = '-1';

            $res = $obj->save();

            if ($res){

                $rerurn = ['code'=>1,'msg'=>'OK'];

            }else{

                $rerurn = ['code'=>0,'msg'=>'服务器错误'];
            }
        }else{

            $rerurn = ['code'=>0,'msg'=>'没有权限'];
        }

        return $rerurn;
    }
}